package com.sora.pet.service.impl;

import com.sora.basic.service.impl.BaiduAiAuditServiceImpl;
import com.sora.basic.util.AjaxResult;
import com.sora.pet.domain.Pet;
import com.sora.pet.domain.PetDetail;
import com.sora.pet.domain.PetOnlineAuditLog;
import com.sora.pet.mapper.PetDetailMapper;
import com.sora.pet.mapper.PetMapper;
import com.sora.pet.mapper.PetOnlineAuditLogMapper;
import com.sora.pet.service.IPetService;
import com.sora.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        petDetailMapper.delByPetId(id);
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    @Override
    @Transactional
    public void batchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //获取需要审核的信息
            String petName = pet.getName();
            String resources = pet.getResources();
            Boolean boo1 = baiduAiAuditService.textAudit(petName);
            Boolean boo2 = baiduAiAuditService.imageAudit(resources);

            if(boo1 && boo2){ //两个审核结果都为true，表示审核通过
                //添加审核日志，更改宠物状态,和上架时间
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setNote(id+"审核成功！");
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                petMapper.updateOnsaleState(ids,new Date());
            }else{
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setNote(id+"审核失败！");
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                return new AjaxResult(false,"上架失败！");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids) {
        //更改宠物状态，添加下架时间
        petMapper.updateOffsaleState(ids,new Date());
    }
}
